package com.baogang.service.impl;

import com.baogang.entity.SwingCardRecord;
import com.baogang.mapper.SwingCardRecordMapper;
import com.baogang.service.SwingCardRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵自强
 * @since 2020-11-20
 */
@Service
@Slf4j
public class SwingCardRecordServiceImpl extends ServiceImpl<SwingCardRecordMapper, SwingCardRecord> implements SwingCardRecordService {


    @Override
    public List<SwingCardRecord> getLastDaysData() {
        return this.list(new QueryWrapper<SwingCardRecord>().lambda()
                .between(SwingCardRecord::getSwingTime, getLastDayStartDate(), getLastDayEndDate()));
    }

    @Override
    public String doPost() {
        long start = System.currentTimeMillis();
        List<SwingCardRecord> lastDaysData = getLastDaysData();
        long queryTime = System.currentTimeMillis();
        log.info("post data size:{}", lastDaysData.size());
        log.info("query user time:{}", queryTime - start);
        RestTemplate template = new RestTemplate();
        String s = template.postForObject("http://localhost:809/api/syn_ai/rec", lastDaysData, String.class);
        long end = System.currentTimeMillis();
        log.info("query use time:{}", end - queryTime);
        log.info("total time: {} ms", end -start);
        log.info("receive result: {}", s);
        return s;
    }

    private Date getLastDayStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date d = cal.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(d);
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return timeFormat.parse(time + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    private Date getLastDayEndDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date d = cal.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(d);
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return timeFormat.parse(time + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = timeFormat.format(new SwingCardRecordServiceImpl().getLastDayStartDate());
        System.out.println(format);
        String format1 = timeFormat.format(new SwingCardRecordServiceImpl().getLastDayEndDate());
        System.out.println(format1);
    }

}
